package com.sky.service.impl.admin;

import com.alibaba.druid.sql.visitor.functions.Locate;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.admin.WorkSpaceMapper;
import com.sky.result.Result;
import com.sky.service.admin.WorkSpaceService;
import com.sky.vo.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;


@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;

    @Override
    public Result<SetmealOverViewVO> overviewSetmeals() {
        Integer discontinuedSetmealCount = workSpaceMapper.getSetmealCountByStatus(StatusConstant.DISABLE);
        Integer soldSetmealCount = workSpaceMapper.getSetmealCountByStatus(StatusConstant.ENABLE);

        SetmealOverViewVO vo = SetmealOverViewVO
                .builder()
                .discontinued(discontinuedSetmealCount)
                .sold(soldSetmealCount)
                .build();

        return Result.success(vo);
    }

    @Override
    public Result<DishOverViewVO> overviewDishes() {
        Integer discontinuedSetmealCount = workSpaceMapper.getDishCountByStatus(StatusConstant.DISABLE);
        Integer soldSetmealCount = workSpaceMapper.getDishCountByStatus(StatusConstant.ENABLE);

        DishOverViewVO vo = DishOverViewVO.builder().discontinued(discontinuedSetmealCount).sold(soldSetmealCount).build();

        return Result.success(vo);
    }

    @Override
    public Result<OrderOverViewVO> overviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        OrderOverViewVO vo = OrderOverViewVO
                .builder()
                .allOrders(workSpaceMapper.getTodayOrderCountByStatus(begin,end,null))
                .cancelledOrders(workSpaceMapper.getTodayOrderCountByStatus(begin,end,Orders.CANCELLED))
                .completedOrders(workSpaceMapper.getTodayOrderCountByStatus(begin,end,Orders.COMPLETED))
                .deliveredOrders(workSpaceMapper.getTodayOrderCountByStatus(begin,end,Orders.CONFIRMED))
                .waitingOrders(workSpaceMapper.getTodayOrderCountByStatus(begin,end,Orders.TO_BE_CONFIRMED))
                .build();

        return Result.success(vo);
    }

    @Override
    public Result<BusinessDataVO> businessData(LocalDateTime begin, LocalDateTime end) {

        Double todayAllOrderCount = Double.valueOf(workSpaceMapper.getTodayOrderCountByStatus(begin,end,null));
        Double todayCompletedCount = Double.valueOf(workSpaceMapper.getTodayOrderCountByStatus(begin,end,Orders.COMPLETED));

        Double orderCompletionRate = (todayAllOrderCount.equals(0.0) || todayCompletedCount.equals(0.0) )
                ? 0.0
                : todayCompletedCount / todayAllOrderCount;

        BusinessDataVO vo = BusinessDataVO
                .builder()
                .newUsers(workSpaceMapper.getTodayNewUserCount(begin,end))
                .orderCompletionRate(orderCompletionRate)
                .turnover(workSpaceMapper.getOrderSumByTimeLine(begin,end))
                .unitPrice(workSpaceMapper.getOrderAvgByTimeLine(begin,end))
                .validOrderCount(todayCompletedCount.intValue())
                .build();

        return Result.success(vo);
    }

}
